#![allow(dead_code)]
pub struct Solution;

impl Solution {
    pub fn is_valid(s: String) -> bool {
        let mut stack = vec![];

        for c in s.chars() {
            match c {
                '(' | '[' | '{' => stack.push(c),
                ')' => {
                    if stack.pop() != Some('(') {
                        return false;
                    }
                }
                ']' => {
                    if stack.pop() != Some('[') {
                        return false;
                    }
                }
                '}' => {
                    if stack.pop() != Some('{') {
                        return false;
                    }
                }
                _ => return false,
            }
        }
        stack.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::Solution;
    #[test]
    fn test_1() {
        assert!(Solution::is_valid("()[]{}".to_string()));
    }
    #[test]
    fn test_2() {
        assert!(!Solution::is_valid("(]".to_string()));
    }
    #[test]
    fn test_3() {
        assert!(!Solution::is_valid("(".to_string()));
    }
    #[test]
    fn test_4() {
        assert!(!Solution::is_valid("((".to_string()));
    }
}
